package net.shao.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.socks.library.KLog;

import net.shao.R;
import net.shao.adapter.ChatListAdapter;
import net.shao.base.BaseActivity;
import net.shao.model.ChatListModel;
import net.shao.widget.ChatListItemLongDialog;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ChatListActivity extends BaseActivity {

    @ViewInject(R.id.listview_chat)
    private ListView listView;
    @ViewInject(R.id.tv_emtryData)
    private TextView tv_emtryData;

    private ChatListAdapter adapter;
    private List<ChatListModel> chatList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_chat_list);
        x.view().inject(this);

        chatList = new ArrayList<>();
        setData();
        adapter = new ChatListAdapter(this, chatList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(ChatListActivity.this, ChatActivity.class);
            intent.putExtra("chatListModel", chatList.get(position));
            startActivity(intent);
        });



        listView.setOnItemLongClickListener((parent, view, position, id) -> {
            ChatListItemLongDialog dialog = new ChatListItemLongDialog(ChatListActivity.this, R.style.ChatListDialog);
            dialog.setItemOnClick(new ChatListItemLongDialog.ItemOnClick() {
                @Override
                public void onReadOnClick() {
                    KLog.i("标记为未读item");
                    dialog.dismiss();
                }

                @Override
                public void onTopOnClick() {
                    KLog.i("置顶item");

                    dialog.dismiss();
                }

                @Override
                public void onRemoveClick() {
                    KLog.i("删除item");
                    chatList.remove(position);
                    adapter.notifyDataSetChanged();
                    if (chatList.isEmpty()){
                        tv_emtryData.setVisibility(View.VISIBLE);
                    }

                    dialog.dismiss();
                }
            });
            dialog.show();

            return true;
        });
    }


    private void setData() {
        ChatListModel listModel1 = new ChatListModel();
        listModel1.setChatId(1);
        listModel1.setImgId(R.mipmap.ic_launcher);
        listModel1.setChatText("今天出来玩");
        listModel1.setChatTime("8.25");
        listModel1.setChatTitle("小明");
        listModel1.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel2 = new ChatListModel();
        listModel2.setChatId(2);
        listModel2.setImgId(R.mipmap.ic_launcher);
        listModel2.setChatText("明天出来玩");
        listModel2.setChatTime("8.26");
        listModel2.setChatTitle("Java统治世界群");
        listModel2.setChatType(ChatListModel.CHAT_LIST_TYPE_GROUP);
        ChatListModel listModel3 = new ChatListModel();
        listModel3.setChatId(3);
        listModel3.setImgId(R.mipmap.ic_launcher);
        listModel3.setChatText("今天天气不错");
        listModel3.setChatTime("8.27");
        listModel3.setChatTitle("小张");
        listModel3.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel4 = new ChatListModel();
        listModel4.setChatId(4);
        listModel4.setImgId(R.mipmap.ic_launcher);
        listModel4.setChatText("明天好像有暴雨啊");
        listModel4.setChatTime("8.25");
        listModel4.setChatTitle("玩Android娱乐群");
        listModel4.setChatType(ChatListModel.CHAT_LIST_TYPE_GROUP);
        ChatListModel listModel5 = new ChatListModel();
        listModel5.setChatId(5);
        listModel5.setImgId(R.mipmap.ic_launcher);
        listModel5.setChatText("哈哈哈哈");
        listModel5.setChatTime("8.25");
        listModel5.setChatTitle("小李");
        listModel5.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel6 = new ChatListModel();
        listModel6.setChatId(5);
        listModel6.setImgId(R.mipmap.ic_launcher);
        listModel6.setChatText("哈哈哈哈");
        listModel6.setChatTime("8.25");
        listModel6.setChatTitle("五老星");
        listModel6.setChatType(ChatListModel.CHAT_LIST_TYPE_GROUP);
        ChatListModel listModel7 = new ChatListModel();
        listModel7.setChatId(5);
        listModel7.setImgId(R.mipmap.ic_launcher);
        listModel7.setChatText("哈哈哈哈");
        listModel7.setChatTime("8.25");
        listModel7.setChatTitle("路飞");
        listModel7.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel8 = new ChatListModel();
        listModel8.setChatId(5);
        listModel8.setImgId(R.mipmap.ic_launcher);
        listModel8.setChatText("哈哈哈哈");
        listModel8.setChatTime("8.25");
        listModel8.setChatTitle("大蛇丸");
        listModel8.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel9 = new ChatListModel();
        listModel9.setChatId(5);
        listModel9.setImgId(R.mipmap.ic_launcher);
        listModel9.setChatText("哈哈哈哈");
        listModel9.setChatTime("8.25");
        listModel9.setChatTitle("小刚");
        listModel9.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel10 = new ChatListModel();
        listModel10.setChatId(5);
        listModel10.setImgId(R.mipmap.ic_launcher);
        listModel10.setChatText("哈哈哈哈");
        listModel10.setChatTime("8.25");
        listModel10.setChatTitle("小霞");
        listModel10.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);
        ChatListModel listModel11 = new ChatListModel();
        listModel11.setChatId(5);
        listModel11.setImgId(R.mipmap.ic_launcher);
        listModel11.setChatText("哈哈哈哈");
        listModel11.setChatTime("8.25");
        listModel11.setChatTitle("小智");
        listModel11.setChatType(ChatListModel.CHAT_LIST_TYPE_FRIEND);

        chatList.add(listModel1);
        chatList.add(listModel2);
        chatList.add(listModel3);
        chatList.add(listModel4);
        chatList.add(listModel5);
        chatList.add(listModel6);
        chatList.add(listModel7);
        chatList.add(listModel8);
        chatList.add(listModel9);
        chatList.add(listModel10);
        chatList.add(listModel11);
    }
}
